﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.ByteBuffer;
using tools;

namespace WindowsFormsApplication2
{
   public class Header
    {
        /** 默认消息头长度 */
    	public static int DEFAULT_HEADER_LENGTH = 7;
	    /** 格式 */
	    public byte format;
	    /** 状态 */
        public short state;
	    /** 序号 */
        public short sn;
	    /** 模块指令 */
        public byte module;
        /** 操作指令*/
        public byte command;

        public static Header ValueOf(byte f, short s, short _sn, byte m, byte c)
        {
            Header header = new Header();
            header.format = f;
            header.state = s;
            header.sn = _sn;
            header.module = m;
            header.command = c;
            return header;
        }

        public static Header Format(MyConverter con)
        {
            Header header = new Header();
            header.format = (byte)con.toInt8();
            header.state = con.toInt16();
            header.sn = con.toInt16();
           
            header.command = (byte)con.toInt8();
			header.module = (byte)con.toInt8();
            return header;
        }

        public String toString()
        {
            return "[F=" + format + ", ST=" + state + ", SN=" + sn + "," + "M="+ module + ",C=" + command + "]";
        }

        public byte[] ToByteArray() {
            ByteBuffer byteList = new ByteBuffer();
            byteList.PushByte(this.format);
            byteList.PushShort(this.state);
            byteList.PushShort(this.sn);
            byteList.PushByte(this.module);
            byteList.PushByte(this.command);
            return byteList.ToByteArray();
        }
    }
}
